package service;

import java.util.*;

/**
 * Class for applications stored in the File Server database, containing
 * application name and required bandwidth
 */
class AppInfo {
    String name;    // Application name
    int bw;         // required bandwidth

    public AppInfo(String appName, int BW) {
        name = appName;
        bw = BW;
    }

    public String getName() {
        return name;
    }
}

/**
 * The file server class creates a database, generates a random number
 * between 0 and 200 as available bandwidth and can take requests for
 * applications and return available bandwidth of server, 0 if not enough
 * or application is not available.
 */
public class FSdb2 {
    private Vector<AppInfo> apps = new Vector<AppInfo>();
    private static Random r = new Random();
    private final static int avBW = r.nextInt(200);

    public int getBW(String appName) {
    	if(this.apps.isEmpty()) init();
    	
        int bw = 0;
        Iterator<AppInfo> it = this.apps.iterator();

        while(it.hasNext()){
            AppInfo application = (AppInfo) it.next();

            if(application.name.equals(appName)) {
                if(this.avBW > application.bw) bw = this.avBW;
            }
        }

        return bw;
    }
    
    private void init() {
    	this.apps.add(new AppInfo("SW1", 10));
        this.apps.add(new AppInfo("SW2", 15));
        this.apps.add(new AppInfo("NetBeans", 30));
        this.apps.add(new AppInfo("Eclipse", 20));
        this.apps.add(new AppInfo("XilinxISE", 150));
    }
}